//
//  PersonalViewController.m
//  Arts1
//
//  Created by 孤星晨宇 on 15/9/18.
//  Copyright (c) 2015年 Arts1. All rights reserved.
//

#import "PersonalViewController.h"
#import "PrativeTextTableViewCell.h"
#import "PraviteImageTableViewCell.h"
#import <QuartzCore/QuartzCore.h>
#import <QuartzCore/CoreAnimation.h>
#import <MobileCoreServices/UTCoreTypes.h>
#import "AllHeader.h"


#define kURL @"http://arts1.dlab.com.cn/app/changeinfo"
@interface PersonalViewController ()<UITableViewDataSource, UITableViewDelegate,UITextFieldDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>

@property (nonatomic, strong) UITableView *mainTableview;

//选择头像的图片
@property (nonatomic ,strong) UIImage *photoImage;

// 左侧标题
@property (nonatomic, strong) NSArray *dataArr;

@end

@implementation PersonalViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationItem.title = @"个人设置";
//    [UIView viewShadowColor:self.navigationController.navigationBar ];
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self createMainTableView];
    // Do any additional setup after loading the view from its nib.
}

// - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    return NO;
}
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if ([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        return NO;
    }
    return YES;
}

//请求数据
- (void)requestData
{
    AFHTTPRequestOperationManager *manage = [AFHTTPRequestOperationManager manager];
    
    NSDictionary *dic = [[NSUserDefaults standardUserDefaults] objectForKey:@"dataForKeyAndUid"];
    
    NSString *uid = dic[@"data"][@"uid"];
    
    NSString *key = dic[@"data"][@"key"];
    
//    NSDictionary *pardic = @{@"uid":uid,
//                             @"key":key,
//                             @"nickName":,
//                             @"gender":,
//                             @"mobile":,
//                             @"userType":,
//                             @"Province":,
//                             @"City":,
//                             @"school":,
//                             @"space":,};
//    
//    [manage POST:kURL parameters:pardic
//         success:^(AFHTTPRequestOperation *operation, id responseObject) {
//             NSString * str = [NSJSONSerialization JSONObjectWithData:responseObject options:(NSJSONReadingMutableContainers) error:nil];
//             
//             // 请求成功的时候调用这个block
//             NSLog(@"数据请求--%@", str);
//         } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//             // 请求失败的时候调用调用这个block
//             NSLog(@"请求失败");
//         }];
}

- (void)createMainTableView
{
    _dataArr = @[@"头像",@"名字",@"性别",@"手机号码",@"身份",@"省份",@"学校",@"画室",@"完成"];
    
    _mainTableview = [[UITableView alloc] initWithFrame:self.view.bounds style:(UITableViewStyleGrouped)];
    _mainTableview.backgroundColor = kCustomColor(250, 250, 250, 1.0);
    _mainTableview.delegate = self;
    _mainTableview.dataSource = self;
    _mainTableview.bounces = NO;
    [self.view addSubview:_mainTableview];
}

#pragma mark --- UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0)
    {
        return 8;
    }
    return 1;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        if (indexPath.row == 0)
        {
            return 70;
        }
        else
        {
            return 40;
        }
    }
    else
    {
        return 40;
    }
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        if (indexPath.row == 0)
        {
            PraviteImageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"imageSetingcell"];
            if (!cell)
            {
                cell = [[PraviteImageTableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"imageSetingcell"];
            }
            
            cell.leftlabel.text = _dataArr[indexPath.row];
            return cell;
        }
        else
        {
            PrativeTextTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pravitecell"];
            if (!cell)
            {
                cell = [[PrativeTextTableViewCell alloc] initWithStyle:(UITableViewCellStyleValue2) reuseIdentifier:@"pravitecell"];
                cell.textLabel.textAlignment = NSTextAlignmentLeft;
                cell.textLabel.textColor = lFONTCOLOR;
            }
            cell.leftLabel.text = _dataArr[indexPath.row];
            return cell;
        }
    }
    else
    {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"wancheng"];
        
        if (cell == nil)
        {
            cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"wancheng"];
        }
        cell.textLabel.text = @"完成";
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
        return cell;
    }
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //取消选中的状态
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    if (indexPath.section == 0){
        if (indexPath.row == 0){
            
            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:(UIAlertControllerStyleActionSheet)];
            UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                
            }];
            UIAlertAction *cameraAction = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                [self shootPiicturePrVideo];
            }];
            UIAlertAction *photoAction = [UIAlertAction actionWithTitle:@"从手机相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                [self selectExistingPictureOrVideo];
            }];
            [alertController addAction:cancelAction];
            [alertController addAction:cameraAction];
            [alertController addAction:photoAction];
            [self presentViewController:alertController animated:YES completion:nil];
        }
    }

}

//从相机上选择
- (void)shootPiicturePrVideo
{
    [self getMediaFromSource:UIImagePickerControllerSourceTypeCamera];
}

//从相册中选择
- (void)selectExistingPictureOrVideo
{
    [self getMediaFromSource:UIImagePickerControllerSourceTypePhotoLibrary];
}

//用户选中图片后的回调
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    
    //获得编辑过的图片
    UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
    
    _photoImage = image;
    
    PraviteImageTableViewCell *cell = [_mainTableview dequeueReusableCellWithIdentifier:@"imageSetingcell"];
    cell.photoImage = image;
    
    [_mainTableview reloadData];
    //如果不选择的话，可以返回原来的页面
    [picker dismissViewControllerAnimated:YES completion:^{
    
    }];
}
// 用户选择取消
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    //如果不选择的话，可以返回原来的页面
    [picker dismissViewControllerAnimated:NO completion:nil];
    
}
//根据用户选取的类型(相册或者相机)来判断当前状态是否支持该功能
-(void)getMediaFromSource:(UIImagePickerControllerSourceType)sourceType
{
    NSArray *mediatypes = [UIImagePickerController availableMediaTypesForSourceType:sourceType];
    
    if([UIImagePickerController isSourceTypeAvailable:sourceType] &&[mediatypes count]>0)
    {
        NSArray *mediatypes = [UIImagePickerController availableMediaTypesForSourceType:sourceType];
        
        UIImagePickerController *picker=[[UIImagePickerController alloc] init];
        
        picker.mediaTypes = mediatypes;
        
        picker.delegate = self;
        
        picker.allowsEditing = YES;
        
        picker.sourceType = sourceType;
        
        NSString *requiredmediatype = (NSString *)kUTTypeImage;
        
        NSArray *arrmediatypes = [NSArray arrayWithObject:requiredmediatype];
        
        [picker setMediaTypes:arrmediatypes];
        
        [self presentViewController:picker animated:YES completion:^{
            
        }];
    }
    else
    {
        //如果不支持拍摄功能就弹出提示
        UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"错误信息!" message:@"当前设备不支持拍摄功能" delegate:nil cancelButtonTitle:@"确认" otherButtonTitles: nil];
        [alert show];
    }
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

@end
